package com.lmscn.lms.api.controller;

import com.jfinal.core.Controller;
import com.lmscn.lms.api.model.SystemMessage;
import com.lmscn.lms.api.model.SystemMsgStudent;
import com.lmscn.lms.api.util.JsonKit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Czl on 2015/9/12. 消息中心接口
 * 
 * @date 2016年11月24日13:13:55
 */
public class MsgCenterController extends Controller {
	/**
	 * 消息中心
	 * 
	 * @date 2016年11月24日13:14:04
	 * @modified 2016年12月9日09:48:56
	 */
	public void sysMsgCenter() {
		Map<String, Object> finalMap = new HashMap<String, Object>();

		Long studentId = getParaToLong("studentId");

		List<SystemMsgStudent> systemMsgStudentList = SystemMsgStudent.dao.findByStuId(studentId);

		List<Map<String, Object>> dataMapList = new ArrayList<Map<String, Object>>();
		for (SystemMsgStudent msgStudent : systemMsgStudentList) {
			Map<String, Object> dataMap = new HashMap<String, Object>();

			SystemMessage systemMessage = SystemMessage.dao.findByMsgId(msgStudent.getLong("sys_msg_id"));

			if (systemMessage != null) {
				dataMap.put("is_read", msgStudent.getInt("is_read"));
				dataMap.put("systemMessage", systemMessage);
				dataMapList.add(dataMap);
			}
		}
		finalMap.put("sysMsg", dataMapList);
		
		renderJson(JsonKit.mapToJson(finalMap, 39));
	}

	/**
	 * 提醒
	 */
	public void sysMsg() {
		Long studentId = getParaToLong("studentId");
		int type = getParaToInt("type");
		Map<String, Object> finalMap = new HashMap<String, Object>();
		List<SystemMsgStudent> systemMsgStudentList = SystemMsgStudent.dao.findByStuId(studentId);
		List<Map<String, Object>> dataMapList = new ArrayList<Map<String, Object>>();
		for (SystemMsgStudent msgStudent : systemMsgStudentList) {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			SystemMessage systemMessage = SystemMessage.dao.findByMsgId(msgStudent.getLong("sys_msg_id"));
			if (systemMessage != null) {
				if (systemMessage.getInt("msg_type") == type) {
					dataMap.put("is_read", msgStudent.getInt("is_read"));
					dataMap.put("systemMessage", systemMessage);
					dataMapList.add(dataMap);
				}
			}
		}
		finalMap.put("sysMsg", dataMapList);
		renderJson(JsonKit.mapToJson(finalMap, 39));
	}

	/**
	 * 读取消息
	 */
	public void readMsg() {
		Long studentId = getParaToLong("studentId");
		Long msgId = getParaToLong("msgId");
		Map<String, Object> dataMap = new HashMap<String, Object>();
		SystemMessage systemMessage = SystemMessage.dao.findById(msgId);
		dataMap.put("systemMessage", systemMessage);
		SystemMsgStudent sysMsgStudent = SystemMsgStudent.dao.findByStuIdAndMsgId(studentId, msgId);
		if (sysMsgStudent.getInt("is_read") == 0) {
			sysMsgStudent.set("is_read", 1);
			sysMsgStudent.update();
		}
		renderJson(JsonKit.mapToJson(dataMap, 39));
	}

	/**
	 * 批量全部阅读
	 */
	public void batchReadMsg() {
		String msgIds = getPara("msgIds");
		Long studentId = getParaToLong("studentId");
		Map<String, Object> dataMap = new HashMap<String, Object>();
		try {
			if (msgIds != null && !msgIds.equals("")) {
				String[] msgIdarray = msgIds.split(",");
				for (String msg : msgIdarray) {
					Long msgId = Long.parseLong(msg);
					SystemMsgStudent sysMsgStudent = SystemMsgStudent.dao.findByStuIdAndMsgId(studentId, msgId);
					if (sysMsgStudent.getInt("is_read") == 0) {
						sysMsgStudent.set("is_read", 1);
						sysMsgStudent.update();
					}
				}
			}
		} catch (Exception e) {
			dataMap.put("error_code", 1);
			dataMap.put("msg", "success");
		}
		dataMap.put("error_code", 0);
		dataMap.put("msg", "success");
		renderJson(JsonKit.mapToJson(dataMap, 39));
	}
}
